package com.hhh.tools.ui.screens

import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.*
import androidx.compose.material.MaterialTheme
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.navigation.NavController
import androidx.navigation.compose.rememberNavController
import com.hhh.tools.R
import com.hhh.tools.ui.navigation.Destinations
import com.hhh.tools.ui.theme.ComposeDemoTheme

@Preview
@Composable
fun PreviewMainPage() {
    val navController = rememberNavController()
    MainPage(navController)
}

@Composable
fun MainPage(navController: NavController) {
    ComposeDemoTheme {
        Column(
            Modifier
                .background(MaterialTheme.colors.background)
                .fillMaxSize()
        ) {
            Box(
                modifier = Modifier
                    .background(Color(0xFF6200EE))
                    .fillMaxWidth()
                    .height(50.dp)
                    .padding(horizontal = 20.dp),
                contentAlignment = Alignment.CenterStart
            ) {
                Text(text = "开发助手", color = Color.White)
            }
            Row(
                modifier = Modifier
                    .fillMaxWidth()
                    .height(90.dp),
                verticalAlignment = Alignment.CenterVertically
            ) {
                ItemMain(
                    "Base64转码",
                    Modifier
                        .fillMaxWidth()
                        .weight(1f)
                        .clickable {
                            navController.navigate(Destinations.Base64Page.route)
                        }
                )
                ItemMain(
                    "MD5加密",
                    Modifier
                        .fillMaxWidth()
                        .weight(1f)
                        .clickable {
                            navController.navigate(Destinations.Md5Page.route)
                        }
                )
            }
            Row(
                modifier = Modifier
                    .fillMaxWidth()
                    .height(90.dp),
                verticalAlignment = Alignment.CenterVertically
            ) {
                ItemMain(
                    "unicode中文互转",
                    Modifier
                        .fillMaxWidth()
                        .weight(1f)
                        .clickable {
                            navController.navigate(Destinations.UniCodePage.route)
                        }
                )
                ItemMain(
                    "URL编码",
                    Modifier
                        .fillMaxWidth()
                        .weight(1f)
                )
                /*ItemMain(
                    "进制转换",
                    Modifier
                        .fillMaxWidth()
                        .weight(1f)
                        .clickable {
                            navController.navigate(Screen.BaseConversionPage.route)
                        }
                )
                Spacer(
                    modifier = Modifier
                        .fillMaxWidth()
                        .weight(1f)
                )*/
            }
        }
    }
}

@Composable
fun ItemMain(title: String, modifier: Modifier = Modifier) {
    Column(
        modifier = modifier,
        horizontalAlignment = Alignment.CenterHorizontally
    ) {
        Image(
            painter = painterResource(id = R.mipmap.default_avatar4),
            contentDescription = title,
            modifier = Modifier.width(50.dp)
        )
        Spacer(modifier = Modifier.width(8.dp))
        Text(text = title, color = Color.Gray, fontSize = 14.sp, maxLines = 1, overflow = TextOverflow.Ellipsis)
    }
}